package com.lz.football.module.techBar.view
{
	import com.common.utils.Reflection;
	import com.global.GlobalDef;
	import com.greensock.TweenLite;
	import com.liangzhong.events.TechBarEvent;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.goods.vo.GoodsVo;
	import com.liangzhong.model.type.GoodsType;
	import com.liangzhong.model.type.OtherGoodType;
	import com.lz.football.module.base.IComponent;
	import com.lz.football.module.techBar.item.TechGoodsItem;
	
	import common.base.GoodsUsePanel;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.text.TextField;
	
	import lz.controls.Alert;
	import lz.controls.events.ListEvent;
	import lz.controls.managers.PopUpManager;
	import lz.controls.managers.TipsManager;

	/**
	 *球员合成 
	 * @author lake
	 * 
	 */	
	public class PlayerCombinePanel extends TechBasePanel implements ITechPanel
	{
		/**
		 *物品显示容器 
		 */		
		private var container 			:MovieClip;
		/**
		 *开始合成 
		 */		
		private var startBtn			:SimpleButton;
		/**
		 *规则 
		 */		
		private var ruleBtn			:SimpleButton;
		/**
		 *放装备等item 
		 */		
		private var itemMc				:MovieClip;
		/**
		 *需要使用的item
		 * 物品位置 
		 */		
		private var useItemList		:Array = [];
		/**
		 *合成使用的物品列表 
		 */		
		private var useGoodsList		:Array = [];
		/**
		 *需要的物品个数 
		 */		
		private var itemCount			:int = 5;
		/**
		 *保护膜位置 
		 */		
		private var safeItem			:TechGoodsItem;
		/**
		 *目标位置 
		 */		
		private var targetItem			:TechGoodsItem;
		/**
		 *显示商品 
		 */		
		private var showGoodsPanel 	:ShowGoodsPanel;
		/**
		 *召集令等级 
		 * 1-7 灰 - 黑
		 */		
		private var callCardLev		:int ;
		/**
		 *卡颜色 
		 */		
		private var colorNames			:Array = ["灰色","绿色","蓝色","紫色","橙色","红色","黑色"];
		/**
		 *成功概率
		 * 2 - 7 
		 */		
		private var sucessRates		:Array = [91,83,71,43,23,13];
		
		public function PlayerCombinePanel()
		{
			super();
		}
		/**
		 *初始化 
		 * 
		 */	
		override public function initUI(view:Object=null):void
		{
			_symbolSkin = Reflection.createInstance("com.football.techbar.view.PlayerCombineSkin");
			this.addChild(_symbolSkin);
			container = _symbolSkin.container;
			itemMc = _symbolSkin.itemMc;
			startBtn = _symbolSkin.startBtn;
			ruleBtn = _symbolSkin.ruleBtn;
			//商品
			showGoodsPanel = new ShowGoodsPanel();
			showGoodsPanel.initUI();
			container.addChild(showGoodsPanel);
			//初始化位置数据
			safeItem = new TechGoodsItem();
			safeItem.initUI(itemMc.item_5);
			targetItem = new TechGoodsItem();
			targetItem.initUI(itemMc.item_6);
			var i :int = 0;
			var tItem :TechGoodsItem;
			for(;i<itemCount;i++)
			{
				tItem = new TechGoodsItem();
				tItem.initUI(itemMc["item_" + i]);
				useItemList.push(tItem);
				tItem.addEventListener(ListEvent.ITEM_CLICK,useItemClickHandler)
			}
			//事件
			addEvtListener();
			//效果
			effectMc = _symbolSkin.effectMc;
			effectMc.visible = false;
		}
		/**
		 *注册事件 
		 * 
		 */	
		override public function addEvtListener():void
		{
			safeItem.addEventListener(ListEvent.ITEM_CLICK,safeItemClickHandler);
			showGoodsPanel.addEventListener(ListEvent.ITEM_CLICK,showItemClickHandler);
			startBtn.addEventListener(MouseEvent.CLICK,startClickHandler);
			ruleBtn.addEventListener(MouseEvent.CLICK,ruleClickHandler);
		}
		/**
		 *规则点击 
		 * @param e
		 * 
		 */		
		override protected function ruleClickHandler(e :MouseEvent):void
		{
			this.dispatchEvent(new TechBarEvent("rule_Click",3,true));
		}
		/**
		 *点击召集令
		 * @param e
		 * 
		 */		
		private function safeItemClickHandler(e :ListEvent):void
		{
			if(safeItem.data)
			{
				callCardLev = 0;
				safeItem.data = null;
				TipsManager.hide();
				return ;
			}
			var goodsUsePanel :GoodsUsePanel = GoodsUsePanel.inistance;
			goodsUsePanel.dragEnabled = true;
			goodsUsePanel.addEvtListener();
			goodsUsePanel.goodsList = AppData.getInstance().bagDataManager.getGoodsListByTwoTypes(GoodsType.OTHER,OtherGoodType.CALLINCARD);
			goodsUsePanel.showBtn(3);
			goodsUsePanel.callBack = safeItemUse;
			var globPoint :Point = this.localToGlobal(new Point(0,0));
			PopUpManager.addWindow(goodsUsePanel,GlobalDef.GameWindowContainer,globPoint.x - 45,globPoint.y,true);
		}
		/**
		 *召集令使用 
		 * @param goodsVo
		 * 
		 */		
		private function safeItemUse(goodsVo :GoodsVo):void
		{
			if(goodsVo == null)
			{
				return;
			}
			callCardLev = goodsVo.color;
			var info :String = "";
			if(isFull)
			{
				//判断召集令
				if(callCardLev != 0)
				{
					if(isFitCallCard == false)
					{
						info = "使用" + goodsVo.name + ",所有球员卡必须为" + colorNames[callCardLev - 2];
						Alert.showErrorTip(info);
						callCardLev = 0;
						return ;
					}
				}
			}
			else
			{
				info = "必须放置"+itemCount+"个球员卡才能进行合成";
				Alert.showErrorTip(info);
				callCardLev = 0;
				return;
			}
			callCardLev = goodsVo.color;
			safeItem.data = goodsVo;
			GoodsUsePanel.inistance.rmoveStage();
		}
		/**
		 *点击左边物品 
		 * @param e
		 * 
		 */		
		private function showItemClickHandler(e :ListEvent):void
		{
			if(isFull)//位置已放满物品
			{
				return;
			}
			var goodsVo :GoodsVo = e.item as GoodsVo;
			if(goodsVo == null)
			{
				return;
			}
			if(goodsVo.color >= 7)
			{
				Alert.showErrorTip("该球员已达顶级，不能合成");
				return ;
			}
			var tItem :TechGoodsItem;
			//左边选中物品显示在右边
			for each(tItem in useItemList)
			{
				if(tItem.data == null)
				{
					tItem.data = goodsVo;
					useGoodsList.push(goodsVo);
					showGoodsPanel.removeGoods(goodsVo);
					break;
				}
			}
			//下一级物品
			var nextColor :int = 1;
			var nextGoods :GoodsVo;
			if(goodsVo)
			{
				nextColor = goodsVo.color + 1;
				if(nextColor > 7)
				{
					return ;
				}
			}
			TipsManager.hide();
		}
		/**
		 *卸下右边已装上的物品 
		 */		
		private function removeUseItem():void
		{
			var tItem :TechGoodsItem;
			//如右边已有物品 则卸下
			for each(tItem  in useItemList)
			{
				if(tItem.data)
				{
					var tempgoodsVo:GoodsVo = tItem.data as GoodsVo;
					showGoodsPanel.addGoods(tempgoodsVo);
					useGoodsList.splice(useGoodsList.indexOf(tempgoodsVo),1);
					tItem.data = null;
				}
			}
		}
		/**
		 *物品使用位置点击 
		 * @param e
		 * 
		 */		
		private function useItemClickHandler(e :ListEvent):void
		{
			var tItem :TechGoodsItem = e.target as TechGoodsItem;
			var goodsVo :GoodsVo;
			if(tItem && tItem.data)
			{
				goodsVo = tItem.data as GoodsVo;
				showGoodsPanel.addGoods(goodsVo);
				useGoodsList.splice(useGoodsList.indexOf(goodsVo),1);
				tItem.data = null;
				TipsManager.hide();
			}
		}
		/**
		 *目标位置点击 
		 * @param e
		 * 
		 */		
		private function targetItemClickHandler(e :ListEvent):void
		{
			targetItem.data = null;
			//如右边已有物品 则卸下
			removeUseItem();
		}
		/**
		 *重设 恢复初始化 
		 * 
		 */		
		override public function reset():void
		{
			//如右边已有物品 则卸下
			removeUseItem();
			safeItem.data = null;
			startBtn.mouseEnabled = true;
			targetItem.data = null;
			for each(var tItem :TechGoodsItem in useItemList)
			{
				tItem.data = null;
			}
			useGoodsList.splice(0);
			callCardLev = 0;
			TipsManager.hide();
			showGoodsPanel.mouseChildren = true;
			showGoodsPanel.mouseEnabled = true;
			showGoodsPanel.goodsList = AppData.getInstance().bagDataManager.getGoodsListByType(GoodsType.PLAYERCARD);
			//效果
			effectMc = _symbolSkin.effectMc;
			effectMc.visible = false;
		}
		/**
		 *开始合成 
		 * @param e
		 * 
		 */		
		private function startClickHandler(e :MouseEvent):void
		{
			var info :String = "";
			if(isFull)
			{
				//判断召集令
				if(callCardLev != 0)
				{
					if(isFitCallCard == false)
					{
						info = "使用" + safeItem.data.name + ",所有球员卡必须为" + colorNames[callCardLev - 2];
						Alert.showErrorTip(info);
						return ;
					}
				}
				//发送服务端
				var id1 :int = (useGoodsList[0] as GoodsVo).pid;
				var id2 :int = (useGoodsList[1] as GoodsVo).pid;
				var id3 :int = (useGoodsList[2] as GoodsVo).pid;
				var id4 :int = (useGoodsList[3] as GoodsVo).pid;
				var id5 :int = (useGoodsList[4] as GoodsVo).pid;
				var id6 :int = 0;
				if(safeItem.data)
				{
					id6 = safeItem.data.pid;
				}
				this.dispatchEvent(new TechBarEvent(TechBarEvent.PLAYER_COMBINE,{id1:id1,id2:id2,id3:id3,id4:id4,id5:id5,id6:id6},true));
				startBtn.mouseEnabled = false;
				showGoodsPanel.mouseChildren = false;
				showGoodsPanel.mouseEnabled = false;
			}
			else
			{
				info = "必须放置"+itemCount+"个球员卡才能进行合成";
				Alert.showErrorTip(info);
			}
		}
		/**
		 *是否已经够足够的物品 
		 * @return 
		 */		
		private function get isFull():Boolean
		{
			for each(var tItem :TechGoodsItem in useItemList)
			{
				if(tItem.data == null)
				{
					return false;
				}
			}
			return true;
		}
		/**
		 *是否符合召集令条件 
		 * @return 
		 * 
		 */		
		private function get isFitCallCard():Boolean
		{
			var gvo :GoodsVo = useItemList[0].data;
			var color :int = gvo.color;
			for each(var tItem :TechGoodsItem in useItemList)
			{
				gvo = tItem.data;
				if((gvo.color + 1)!=callCardLev || color != gvo.color)
				{
					return false;
				}
			}
			return true;
		}
		/**
		 *删除事件 
		 * 
		 */	
		override public function removeEvtListener():void
		{
			safeItem.removeEventListener(ListEvent.ITEM_CLICK,safeItemClickHandler);
			showGoodsPanel.removeEventListener(ListEvent.ITEM_CLICK,showItemClickHandler);
			targetItem.removeEventListener(ListEvent.ITEM_CLICK,targetItemClickHandler);
			startBtn.removeEventListener(MouseEvent.CLICK,startClickHandler);
		}
		/**
		 *销毁 
		 * 
		 */	
		override public function destroyView():void
		{
			removeEvtListener();
			for each(var tItem :TechGoodsItem in useItemList)
			{
				tItem.removeEventListener(ListEvent.ITEM_CLICK,useItemClickHandler)
			}
			useItemList = null;
			useGoodsList = null;
			showGoodsPanel.destroyView();
			showGoodsPanel = null;
			TweenLite.killTweensOf(reset);
			TweenLite.killTweensOf(showResObj);
			TweenLite.killTweensOf(showAlertRes);
		}
		/////////////////////////server return//////////////////////////////////////////
		/**
		 *球员合成 
		 * 
		 */		
		public function playerCombine(value :Object):void
		{
			if(value.retcode == 0)
			{
				TweenLite.delayedCall(2,showAlertRes,[value.pid]);
				if(effectMc)
				{
					effectMc.visible = true;
					effectMc.gotoAndPlay(1);
				}
				TweenLite.delayedCall(3,reset);
				
				var pid 	:int = value.pid;
				var ntype 	:int = value.ntype;
				TweenLite.delayedCall(2,showResObj,[pid,ntype]);
			}
			else
			{
				reset();
			}
		}
		/**
		 *显示成功物品 
		 * @param pid
		 * @param ntype
		 * 
		 */		
		private function showResObj(pid:int,ntype:int):void
		{
			if(pid == 0)
			{
				return ;
			}
			var goodsV :GoodsVo = AppData.getInstance().goodsManager.getGoodsByTypeAndId(ntype,pid);
			targetItem.data = goodsV;
		}
		/**
		 *显示成功失败 
		 * @param value
		 * 
		 */		
		private function showAlertRes(value :int ):void
		{
			effectMc.visible = false;
			effectMc.gotoAndStop(1);
			if(value == 0)
			{
				Alert.showErrorTip("球星合成失败");
			}
			else
			{
				Alert.showErrorTip("球星合成成功");
			}
		}
	}
}